package org.crackjack.android.activity;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.crackjack.android.R;
import org.crackjack.android.async.ITaskFinishedHandler;
import org.crackjack.android.async.ProgressDialogAsyncTask;
import org.crackjack.android.util.DeviceUtil;
import org.crackjack.android.util.HttpClient;
import org.crackjack.android.util.ResponseJsonParser;
import org.crackjack.android.util.UrlBuilder;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegistrationActivity extends GenericActivity implements OnClickListener {

    private Button registrationButton;

    private EditText loginEditText;
    private EditText passwordEditText;
    private EditText confirmEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        registrationButton = (Button) findViewById(R.id.button_registration);

        loginEditText = (EditText) findViewById(R.id.edit_text_login);
        passwordEditText = (EditText) findViewById(R.id.edit_text_password);
        confirmEditText = (EditText) findViewById(R.id.edit_text_password_confirm);

        registrationButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_registration:
                if (DeviceUtil.hasInternetConnection(this)) {

                    if (passwordEditText.getText().toString().equals(confirmEditText.getText().toString())) {
                        ITaskFinishedHandler<String> registrationHandler = new ITaskFinishedHandler<String>() {

                            @Override
                            public void onTaskFinished(String result) {
                                if (result.equals("success")) {
                                    showToast("Registration success");
                                    finish();
                                } else {
                                    showToast("Registration error");
                                }
                            }
                        };
                        new RegistrationAsyncTask(this, true, "Registration...", registrationHandler).execute();
                    } else {
                        Toast.makeText(this, "Passwords are not equal", 2000).show();
                    }
                } else {
                    showToast("Need Internet connection");
                }
                break;

            default:
                break;
        }
    }

    private class RegistrationAsyncTask extends ProgressDialogAsyncTask<Void, Void, String> {

        public RegistrationAsyncTask(Activity activity, boolean isProgressBarDialog, String dialogMessage,
                ITaskFinishedHandler<String> handler) {
            super(activity, isProgressBarDialog, dialogMessage, handler);
        }

        @Override
        protected String saveDoInBackground(Void... params) {
            HttpClient client = new HttpClient();
            String authorizationStatus = null;
            String json = null;
            try {
                json = client.makeGetRequest(UrlBuilder.buildRegistrationUrl(
                        loginEditText.getEditableText().toString(), passwordEditText.getEditableText().toString()));
                authorizationStatus = ResponseJsonParser.parseAuthorizationJson(json);

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return authorizationStatus;
        }

    }

}
